package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.BudgetSubItem;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Form;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.UserInfo;
import com.czwj.service.BudgetMainService;
import com.czwj.service.BudgetSubItemService;
import com.czwj.service.FormService;
import com.czwj.service.ProjectItemService;
import com.czwj.util.TextUtil;
import com.czwj.util.WechatUtil;

import net.sf.json.JSONObject;

@Controller
public class BudgetSubItemAction{
	@Resource
	BudgetSubItemService budgetSubItemService;
	
	@Resource
	BudgetMainService budgetMainService;
	
	@Resource
	ProjectItemService projectItemService;
	
	@Resource
	FormService formService;
	
	@Value("${review_model_code}")
	private String model_code;
	
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
	
	@RequestMapping(value = "/subitembudget.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetSubItem> budgetSubItem(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "budgetmainid",required = true) Integer budgetmainid){
		DataGridData<BudgetSubItem> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		map.put("userid",((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		map.put("budgetmainid",budgetmainid);
		List<BudgetSubItem> list = budgetSubItemService.searchbylevel3(map);
		result.setRows(list);
		result.setTotal(budgetSubItemService.countbylevel3(map));
		return result;
	}
	
	@RequestMapping(value = "/budgetsubitems.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetSubItem> budgetsubitems(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "projectitemid",required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid",required = false) String projectitemchildid){
		DataGridData<BudgetSubItem> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)){
			map.put("projectitemchildid",projectitemchildid);
		}
		List<BudgetSubItem> list = budgetSubItemService.search(map);
		result.setRows(list);
		result.setTotal(budgetSubItemService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/budgetSubItemlist.do",method = RequestMethod.POST)
	@ResponseBody
	public List<BudgetSubItem> budgetSubItemlist(@RequestParam(value = "status",required = false) String status,
			@RequestParam(value = "projectitemid",required = false) int projectitemid,
			@RequestParam(value = "bmstatus",required = false) String bmstatus){
		Map<String,Object> map = new HashMap<String,Object>();
		Subject currentUser = SecurityUtils.getSubject();
		map.put("userid",((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		if(projectitemid > 0){
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("projectitemid",projectitemid);
			List<BudgetMain> budgetmains = budgetMainService.searchbyprojectitem(map2);
			if(budgetmains.size() == 1){
				map.put("budgetmainid",budgetmains.get(0).getId());
			}
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status",status);
		}
		if(TextUtil.isNotEmpty(bmstatus)){
			map.put("bmstatus",bmstatus);
		}
		List<BudgetSubItem> list = budgetSubItemService.searchbylevel3(map);
		return list;
	}
	
	@SystemControllerLog(description = "修改预算明细")
	@RequestMapping(value = "/budgetSubItemSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult budgetSubItemSave(@RequestBody BudgetSubItem budgetSubItem,HttpServletRequest request)
			throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		if(budgetSubItem.getId() != null && budgetSubItem.getId() > 0){
			Subject currentUser = SecurityUtils.getSubject();
			budgetSubItem.setUpdaterid(((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			budgetSubItem.setUpdatedate(new Date());
			budgetSubItem.setUpdateip(request.getRemoteAddr());
			count = budgetSubItemService.update(budgetSubItem);
		}else{
			Subject currentUser = SecurityUtils.getSubject();
			budgetSubItem.setCreaterid(((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			budgetSubItem.setCreatedate(new Date());
			budgetSubItem.setCreateip(request.getRemoteAddr());
			budgetSubItem.setStatus(6);
			count = budgetSubItemService.insert(budgetSubItem);
		}
		budgetSubItem = budgetSubItemService.selectByPrimaryKey(budgetSubItem.getId());
		ProjectItem projectItem = projectItemService.selectByPrimaryKey(budgetSubItem.getMain().getProjectitemid());
		for(UserInfo user:projectItem.getLevel2()){
			JSONObject data = WechatUtil.setReviewData(projectItem.getName() + " 预算申请",sdf2.format(new Date()));
			sendReview(user,data);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "禁用预算明细")
	@RequestMapping(value = "budgetSubItemDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult deleteBudgetSubItem(@RequestParam("budgetSubItemid") int id){
		OperateResult result = new OperateResult();
		try{
			int count = budgetSubItemService.updateStatus(id);
			result.setSuccess(count > 0);
			return result;
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@SystemControllerLog(description = "删除预算明细")
	@RequestMapping(value = "budgetSubItemRemove.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult removeBudgetSubItem(@RequestParam("id") int id){
		OperateResult result = new OperateResult();
		int count = budgetSubItemService.remove(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "/review/budgetsubitem.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetSubItem> reviewPlanMain(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<BudgetSubItem> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		int roleid = userInfo.getRoleid();
		if(roleid == 1 || roleid == 2){
			map.put("status",7);
			result.setRows(budgetSubItemService.searchbylevel1(map));
			result.setTotal(budgetSubItemService.countbylevel1(map));
		}
		if(roleid == 3){
			map.put("status",6);
			result.setRows(budgetSubItemService.searchbylevel2(map));
			result.setTotal(budgetSubItemService.countbylevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/reviewed/budgetsubitem.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetSubItem> reviewedPlanMain(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "startdate",required = false) String startdate,
			@RequestParam(value = "enddate",required = false) String enddate,
			@RequestParam(value = "projectitemid",required = false) String projectitemid){
		DataGridData<BudgetSubItem> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate",startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate",enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid) && !projectitemid.equals("0")){
			map.put("projectitemid",projectitemid);
		}
		int roleid = userInfo.getRoleid();
		if(roleid == 1 || roleid == 2){
			map.put("status",4);
			result.setRows(budgetSubItemService.searchbylevel1(map));
			result.setTotal(budgetSubItemService.countbylevel1(map));
		}
		if(roleid == 3){
			map.put("status","4,7");
			result.setRows(budgetSubItemService.searchbylevel2(map));
			result.setTotal(budgetSubItemService.countbylevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/budgetSubItemReviewed.do",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetSubItem> budgetSubItemReviewed(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<BudgetSubItem> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid",userInfo.getId());
		if(userInfo.getRoleid() == 1 || userInfo.getRoleid() == 2){
			map.put("status","3,4");
			result.setRows(budgetSubItemService.searchReviewedLevel1(map));
			result.setTotal(budgetSubItemService.countReviewedLevel1(map));
		}else if(userInfo.getRoleid() == 3){
			map.put("status","3,4,7");
			result.setRows(budgetSubItemService.searchReviewedLevel2(map));
			result.setTotal(budgetSubItemService.countReviewedLevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/getbudget.do",method = RequestMethod.GET)
	@ResponseBody
	public List<BudgetSubItem> getbudget(@RequestParam(value = "projectitemid",required = true) int projectitemid,
			@RequestParam(value = "itemid",required = true) int itemid,
			@RequestParam(value = "status",required = true) String status){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("projectitemid",projectitemid);
		map.put("itemid",itemid);
		map.put("status",status);
		List<BudgetSubItem> list = budgetSubItemService.getbudget(map);
		return list;
	}
	
	private void sendReview(UserInfo userInfo,JSONObject data){
		List<Form> forms = formService.findbyuser(userInfo.getId());
		for(Form form:forms){
			String code = WechatUtil.pushMessage(userInfo.getOpenid(),model_code,form.getFormid(),data);
			formService.deleteByPrimaryKey(form.getId());
			if(code.trim().equals("0")){
				return;
			}
		}
	}
}
